package com.example.administrator.yuejian35.Club.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.administrator.yuejian35.Club.activity.SearchClubActivity;
import com.example.administrator.yuejian35.Club.adapter.ClubListAdapter;
import com.example.administrator.yuejian35.Club.bean.Club;
import com.example.administrator.yuejian35.Club.model.ClubModel;
import com.example.administrator.yuejian35.R;
import com.example.administrator.yuejian35.base.BaseFragment;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * A simple {@link Fragment} subclass.
 */
public class ClubFragment extends BaseFragment {
    private android.support.v7.widget.RecyclerView club_rv;
    private List<Club> list;
    private EditText search;
    private String get;
    private ImageView button;

    public ClubFragment() {
        // Required empty public constructor
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        club_rv= (android.support.v7.widget.RecyclerView) view.findViewById(R.id.club_rv);
        search= (EditText) view.findViewById(R.id.search);
        button= (ImageView) view.findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                get=search.getText().toString();

                Intent intent = new Intent(getActivity(), SearchClubActivity.class);
                intent.putExtra("get",get);
                getActivity().startActivity(intent);



            }
        });
        ClubModel clubmodel = new ClubModel();
        Call call = clubmodel.getallclubs();
        call.enqueue(new Callback<List<Club>>() {
            @Override
            public void onResponse(Call<List<Club>> call, Response<List<Club>> response) {
                if(getActivity()!=null) {
                   list = response.body();
                   /* Toast.makeText(getActivity(),"111",Toast.LENGTH_LONG).show();
                    Toast.makeText(getActivity(),list.get(1).getClubname().toString()+"11111111",Toast.LENGTH_LONG).show();*/
                     ClubListAdapter adapter = new ClubListAdapter(getActivity(), list);

                    LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
                    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);//Horizontal

                    //Grid
                    club_rv.setLayoutManager(layoutManager);
                    //club_rv.addItemDecoration(new RecyclerViewDivider(this, DividerItemDecoration.VERTICAL_LIST));
                    //club_rv.addItemDecoration(new RecyclerViewDivider(getActivity(), LinearLayoutManager.VERTICAL));
                    //club_rv.addItemDecoration(new RecyclerViewDivider(getActivity(), LinearLayoutManager.VERTICAL, 10, ContextCompat.getColor(getActivity(), R.color.colorAccent)));
                    club_rv.setAdapter(adapter);
                    club_rv.setItemAnimator(new DefaultItemAnimator());
                }
            }

            @Override
            public void onFailure(Call<List<Club>> call, Throwable t) {
                Toast.makeText(getActivity(),"查询失败", Toast.LENGTH_LONG).show();
            }
        });
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_club, container, false);

    }

}
